import { Component, OnInit, Input, Output, EventEmitter, ElementRef } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import { config } from '../smartadmin.config';
import { Router } from "@angular/router";

declare var $: any;
@Component({
	selector: 'sa-sensor-type',
	styleUrls: ['./sensorType.css'],
	templateUrl: './sensorType.component.html',
})
export class SensorTypeComponent implements OnInit {
	@Input() sensorTypeId: string = ''; //标识Id
	@Input() Height: number; //高度
	@Input() Width: string = 'auto'; //高度
	@Input() type: string = '1'; //默认select 2为纯渲染span
	@Input() isShow: boolean = true;
	@Input() inputName: string = 'sensorName'; //校验使用的名称;默认name="companyName"
	@Input() inputParam: any = []; //接口参数
	@Input() isSendRequest: boolean = true; //接口参数
	@Output() sensorSelected = new EventEmitter();
	private sensorName: string = ''; //选中的传感器名称
	private sensorType: string = ''; //选中的传感器type

	constructor(private jsonApiService: JsonApiService, private el: ElementRef, private router: Router) {}
	//传参给父组件
	sendParent() {
		let sensorInfo = {
			'sensorType': this.sensorType,
			'sensorName': this.sensorName,
			'inputParam': this.inputParam
		}
		this.sensorSelected.emit(sensorInfo);
	}
	ngOnInit() {
		var that = this;
		if(that.inputParam.length ==0 && this.isSendRequest){
			that.initSensorList()
		}
		
	}
	ngOnChanges() {
		var that = this;
		if(that.sensorTypeId && that.sensorTypeId != 'null') {
			if(that.inputParam.length != 0){
				for(let i=0;i< that.inputParam.length;i++){
					if(that.inputParam[i].sensorCode == that.sensorTypeId){
						that.sensorName = that.inputParam[i].sensorName
					}
				}
			}else{
				if(that.isSendRequest){
					that.initSensorList().then(() => {
						for(let i=0;i< that.inputParam.length;i++){
							if(that.inputParam[i].sensorCode == that.sensorTypeId){
								that.sensorName = that.inputParam[i].sensorName
							}
						}
					})
				}
				
			}
			that.sensorType = that.sensorTypeId;
		}else{
			that.sensorType = '';
			if(that.type =='2'){
				that.sensorName = '--';
			}
			
		}
	}
	ngOnDestroy() {
	}
	//拉取传感器数据
	initSensorList() {
		var that = this;
		if(that.inputParam.length == 0 && that.isSendRequest){
			return new Promise(function (resolve, reject) {
				that.jsonApiService.http('sensorManage/querySensorList.html','').subscribe(data=> {
					if(data.success){
						that.inputParam = data.data
					}
					resolve();
				})
			})
		}
		
		
	}
	selectSensor(data){
		this.sensorType = data;
		for(let i=0;i< this.inputParam.length;i++){
			if(this.inputParam[i].sensorCode == data){
				this.sensorName = this.inputParam[i].sensorName
			}
		}
		this.sendParent();
	}



	// 注释
	/**
	 * 
	 * 
	 * 
	 *  '异常警报','损坏通道比对[算法/人工]              2     调用1次
		'算法管理','Python算法传感器检测结果'         2     调用1次
		'异常警报','传感器损坏明细[人工]'                 2     调用1次
		'车辆定位管理','车辆状态[客户]'                    1      调用1次
		'运输任务','订单监控[GPS]'                           1      调用1次
		'运输任务','订单监控'                                   4     调用1次
		'设备安装管理','安装管理[外包]','安装车辆'     2     调用1次
		'环卫运维管理','问题分析处理'                      3     调用1次
		'系统基础管理','车辆管理'                            2      调用1次
		'系统基础管理','车辆管理','编辑车辆'             1      调用1次
		'系统基础管理','车辆管理','新增车辆'             1      调用2次
		'系统基础管理','项目车辆'                            1      调用1次
		'系统配置','车辆运行状态'                            2     调用1次
		'系统配置','数据优化'                                  3     调用1次
		'项目管理','数据审核'                                  1      调用1次

		[inputParam]="inputParam" [isSendRequest]='false'
		sa-sensor-type

		//请求传感器类型的接口
			querySensor(){
				this.jsonApiService.http('sensorManage/querySensorList.html','').subscribe(data=> {
					if(data.success){
						this.inputParam = data.data
					}
				})
			}
			inputParam: any = []; 

		if(this.inputParam.length == 0){this.querySensor();}// 请求传感器列表接口
	 */
}